package cn.itcast.controller;

import cn.itcast.mapper.RoomMapper;
import cn.itcast.DTO.RoomDTO;
import cn.itcast.entity.Result;
import cn.itcast.entity.Room;
import cn.itcast.util.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@CrossOrigin
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @Autowired
    private RoomMapper roomMapper;

    @PostMapping("/upload/image")
    public Result upload(RoomDTO roomDTO) throws IOException {
        //调用阿里云OSS工具类，将上传上来的文件存入阿里云
        String url = aliOSSUtils.upload(roomDTO.getImages());
        Room room = new Room();
        room.setImages(url);
        room.setId(roomDTO.getId());
        roomMapper.update(room);
        //将图片上传完成后的url返回，用于浏览器回显展示
        return Result.success(url);
    }


}
